package com.itheima.d_config;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet(urlPatterns = "/config")
public class ServletConfigDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ServletConfigDemo执行了...");
        // 获取ServletConfig对象,获取当前Servlet的配置信息
        ServletConfig servletConfig = getServletConfig();
        // TODO:通过servletConfig获取上下文对象
        servletConfig.getServletContext();

        String servletName = servletConfig.getServletName();
        System.out.println("注册给tomcat的名称为: "+servletName);
        String str1 = servletConfig.getInitParameter("str1");
        String str2 = servletConfig.getInitParameter("str2");
        System.out.println(str1+ " : "+str2);
        Enumeration<String> names = servletConfig.getInitParameterNames();
        while (names.hasMoreElements()){
            System.out.println(names.nextElement());
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
